package com.example.gymmanagement.controller;
import com.example.gymmanagement.Utils.Result;
import com.example.gymmanagement.Utils.StatusCode;
import com.example.gymmanagement.domain.Equip;
import com.example.gymmanagement.service.EquipService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/equip")
public class EquipController {
    @Autowired
    private EquipService equipService;

    //新增器材
    @PostMapping("/add")
    @ApiOperation("新增器材")
    public Result add(@RequestBody Equip equip,Integer count,@RequestHeader("Authorization") String token){
        //设置创建时间
        equip.setEquipCreatetime(LocalDateTime.now());
        equip.setEquipStatus("闲置");
        Result result = this.equipService.insert(equip,count,token);
        return result;
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("按id删除器材")
    public Result delete(@PathVariable Integer id,@RequestHeader("Authorization") String token){
        Result result = this.equipService.delete(id);
        return result;
    }
    @PostMapping("/update")
    @ApiOperation("更新器材")
    public Result update(@RequestBody Equip equip,@RequestHeader("Authorization") String token){
        equip.setEquipUpdatetime(LocalDateTime.now());
        boolean flag = this.equipService.updateEquip(equip);
        String msg = flag?"更新成功":"更新失败";
        return new Result(flag?StatusCode.UPDATE_OK:StatusCode.UPDATE_ERR,flag,msg);
    }
    @PostMapping("/repair")
    @ApiOperation("报修器材")
    public Result repair(long equipId,double repairPrice,@RequestHeader("Authorization") String token){
        return this.equipService.repair(equipId,repairPrice,token);
    }

    @GetMapping("/{id}")
    @ApiOperation("按id查询单个器材")
    public Result selectById(@PathVariable Integer id,@RequestHeader("Authorization") String token){
        Equip equip = this.equipService.selectById(id);
        String msg = equip==null?"查询失败":"查询成功";
        return new Result(equip==null?StatusCode.GET_OK:StatusCode.GET_ERR,equip,msg);
    }

    @GetMapping("/unused")
    @ApiOperation("按类别查询闲置的器材(分页)")
    public Result selectUnused(String equipName,Integer index,Integer size,@RequestHeader("Authorization") String token){
        Result equipList = this.equipService.selectUnused(equipName,index,size);
        return equipList;
    }
    @GetMapping("/selectAll")
    @ApiOperation("查询全部器材")
    public Result selectAll(Integer index,Integer size,@RequestHeader("Authorization") String token){
        Result result = equipService.selectAll(index,size);
        return result;
    }
    @PostMapping("/rent")
    @ApiOperation("用户租借器材/包含赛事（0默认不是赛事）")
    public Result rentEquip(long equipId,long matchId,@RequestHeader("Authorization") String token){
        Result result = this.equipService.rent(equipId,matchId,token);
        return result;
    }

    @PostMapping("/return")
    @ApiOperation("用户归还器材")
    public Result returnEquip(long equipId,Integer broken,@RequestHeader("Authorization") String token){
        Result result = this.equipService.returnEquip(equipId,broken,token);
        return result;
    }
//    @GetMapping("/selectByTime")
//    @ApiOperation("按时间查")
//    public Result selectByTime(String start,String end,@RequestHeader("Authorization") String token){
//        Result result = this.equipService.selectByTime(start,end,token);
//        return result;
//    }

}
